﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using uTrade.Common;
using uTrade.Data;

namespace uTrade.Views
{
    //4.定义事件参数类
    public class ItemkArgs : EventArgs
    {
        public readonly Rank RankItem;

        public ItemkArgs(Rank oItem)
        {
            RankItem = oItem;
        }
    }

    /// <summary>
    /// FundDetailView.xaml 的交互逻辑
    /// </summary>
    public partial class FundDetailView : UserControl
    {
        private List<Rank> lstRankAll;
        private PriceInfo fdpInfo;
        private bool IsShowFilteredItems = false;
        private List<EquityModel> ntWorkModel = new List<EquityModel>();

        //1.定义delegate

        public delegate void DoubleClickEventHandler(object sender, ItemkArgs e);

        //2.用event 关键字声明事件对象

        public event DoubleClickEventHandler ItemDoubleClick;

        //3.事件触发方法
        protected virtual void OnItemDoubleClick(ItemkArgs e)
        {
            if (ItemDoubleClick != null)
            {
                ItemDoubleClick(this, e);
            }
        }

        public FundDetailView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void DetailGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataGrid dgSender = sender as DataGrid;
            Rank dRank = dgSender.SelectedItem as Rank;
            string symbol = dRank.Symbol;
            fdpInfo = GetFundEquityInfo.GetFormatedFundInfo(symbol, DateService.ThisTimeLastYear(), DateTime.Now);

            ItemkArgs itemkArgs = new ItemkArgs(dRank);
            //引发事件
            OnItemDoubleClick(itemkArgs);
        }

        /// <summary>
        /// 根据基金代码获取基金详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetFundDetailByID_Click(object sender, RoutedEventArgs e)
        {
            ntWorkModel = GetFundEquityInfo.Info(tbxSrhDetailFundID.Text, DateService.ThisTimeLastYear(), DateTime.Now);
        }

        private void btnSyncFromRemote_Click(object sender, RoutedEventArgs e)
        {
            //Thread tdUpdateRank = new Thread(SyncRankDataFromRemote);
            //tdUpdateRank.Start();
            //pgbSync.Value = 0;
        }

        private void btn_Favorite_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btn_FavoriteFilter_Click(object sender, RoutedEventArgs e)
        {
            if (IsShowFilteredItems)
            {
                IsShowFilteredItems = false;
                ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/favorite.png", UriKind.RelativeOrAbsolute));
                Image imgtemp = new Image();
                imgtemp.Source = imageUpnew;
                imgtemp.Width = 16;
                imgtemp.Height = 16;
                btn_FavoriteFilter.Content = imgtemp;
                lstRankAll = GetFundEquityInfo.GetFavoriteRankList();
                DetailGrid.ItemsSource = lstRankAll;
            }
            else
            {
                IsShowFilteredItems = true;
                ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/un-favorite.png", UriKind.RelativeOrAbsolute));
                Image imgtemp = new Image();
                imgtemp.Source = imageUpnew;
                imgtemp.Width = 16;
                imgtemp.Height = 16;
                btn_FavoriteFilter.Content = imgtemp;
                lstRankAll = GetFundEquityInfo.GetAllRankList();
                DetailGrid.ItemsSource = lstRankAll;
            }
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            //if (null == lstRankAll)
            //{
            //    lstRankAll = GetFundEquityInfo.GetAllRankList();
            //    DetailGrid.ItemsSource = lstRankAll;
            //}
        }
    }
}